﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace Galaktika.BI.Silverlight.Controls.MemberChoice
{
    public partial class CustomPopupControl : UserControl
    {
        public CustomPopupControl()
        {
            InitializeComponent();
            m_PopupControl = new Popup();
            LayoutRoot.Children.Add(m_PopupControl);
        }

        Popup m_PopupControl = null; 

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            HandlePopUp();
        }

        public UIElement ContentControl
        {
            set {
                m_PopupControl.Child = value;    
            }
            get {
                return m_PopupControl.Child;
            }
        }

        public bool IsDropDownOpen
        {
            get {
                return m_PopupControl.IsOpen;
            }
            set {
                if (value)
                {
                    m_PopupControl.VerticalOffset = this.ActualHeight;
                    m_PopupControl.HorizontalOffset = 0;
                }
                
                m_PopupControl.IsOpen = value;
            }
        }

        private void HandlePopUp()
        {
            if (this.IsDropDownOpen)
            {
                base.Focus();
                this.IsDropDownOpen = false;
                this.ContentControl.ReleaseMouseCapture();
            }
            else
            {
                this.IsDropDownOpen = true;
                this.ContentControl.CaptureMouse();
            }
        }
    }
}
